package com.umeng.analytics.pro;

import android.content.Context;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class at {
    private static final String a = "fs_lc_tl";
    private final int b = 128;
    private final int c = 256;
    private final int d = 10;
    private Context e;
    private ar f;
    private aq g;
    private JSONObject h;
    private ar i;

    public at(Context arg3) {
        this.f = null;
        this.g = null;
        this.h = null;
        if (arg3 == null) {
            try {
                by.e("Context is null, can\'t track event");
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
        this.i = ar.b(arg3);
        this.e = arg3;
        this.f = ar.b(this.e);
        this.g = this.f.a(this.e);
        if (this.h != null) {
            return;
        }
        this.a(arg3);
    }

    public void a(String arg6, String arg7, long arg8, int arg10) {
        try {
            if (!this.a(arg6)) {
                return;
            }

            if (!this.b(arg7)) {
                return;
            }

            JSONObject v0_1 = new JSONObject();
            v0_1.put("id", arg6);
            v0_1.put("ts", System.currentTimeMillis());
            if (arg8 > 0) {
                v0_1.put("du", arg8);
            }

            v0_1.put("__t", 2049);
            if (arg7 == null) {
                arg7 = "";
            }

            v0_1.put(arg6, arg7);
            v0_1.put("__i", bd.g(this.e));
            v0_1.put("_umpname", ap.a);
            this.i.a(v0_1);
        } catch (Throwable v0) {
        }
    }

    public void a(String arg8, Map arg9, long arg10) {
        try {
            if ((this.a(arg8)) && (this.b(arg9))) {
                JSONObject v2 = new JSONObject();
                v2.put("id", arg8);
                v2.put("ts", System.currentTimeMillis());
                if (arg10 > 0) {
                    v2.put("du", arg10);
                }

                v2.put("__t", 2049);
                Iterator v3 = arg9.entrySet().iterator();
                int v1;
                for (v1 = 0; v1 < 10; ++v1) {
                    if (!v3.hasNext()) {
                        break;
                    }

                    Object v0_1 = v3.next();
                    if (!"$st_fl".equals(((Map.Entry) v0_1).getKey()) && !"dplus_st".equals(((Map.Entry) v0_1).getKey()) && !"du".equals(((Map.Entry) v0_1).getKey()) && !"id".equals(((Map.Entry) v0_1).getKey()) && !"ts".equals(((Map.Entry) v0_1).getKey())) {
                        Object v4 = ((Map.Entry) v0_1).getValue();
                        if (!(v4 instanceof String) && !(v4 instanceof Integer) && !(v4 instanceof Long)) {
                        } else {
                            v2.put((String) ((Entry) v0_1).getKey(), v4);
                        }
                    }
                }

                v2.put("__i", bd.g(this.e));
                v2.put("_umpname", ap.a);
                this.i.a(v2);
            }

            return;
        } catch (Throwable v0) {
            return;
        }
    }

    public void a(Context arg2, List arg3) {
        try {
            if (this.e != null) {
                return;
            }

            if (arg2 == null) {
                return;
            }

            this.e = arg2;
        } catch (Exception v0) {
        }
    }

    public boolean a(List<String> arg9, int arg10, String arg11) {
        String v4;
        Object v0_2;
        ArrayList v1;
        int v6 = 16;
        int v2 = 0;
        try {
            n v3 = n.a();
            if (arg9 == null) {
                by.e("cklist is null!");
                return true;
            }

            if (arg9.size() <= 0) {
                by.e("the KeyList is null!");
                boolean v0_1 = false;
                return v0_1;
            }

            v1 = new ArrayList(arg9);
            if (!v3.b((String) ((List) v1).get(0))) {
                by.e("Primary key Invalid!");
                return false;
            }

            if (((List) v1).size() > 8) {
                v0_2 = ((List) v1).get(0);
                ((List) v1).clear();
                ((List) v1).add(v0_2);
                ((List) v1).add("__cc");
                ((List) v1).add("illegal");
            } else if (!v3.a(((List) v1))) {
                v0_2 = ((List) v1).get(0);
                ((List) v1).clear();
                ((List) v1).add(v0_2);
                ((List) v1).add("__cc");
                ((List) v1).add("illegal");
            } else {
                if (!v3.b(((List) v1))) {
                    v0_2 = ((List) v1).get(0);
                    ((List) v1).clear();
                    ((List) v1).add(v0_2);
                    ((List) v1).add("__cc");
                    ((List) v1).add("illegal");
                } else {
                    while (true) {
                        label_75:
                        if (v2 >= ((List) v1).size()) {
                            break;
                        }

                        v0_2 = ((List) v1).get(v2);
                        if (((String) v0_2).length() > v6) {
                            ((List) v1).remove(v2);
                            ((List) v1).add(v2, ((String) v0_2).substring(0, 16));
                        }
                        ++v2;
                        continue;
                    }
                }
            }
            if (!v3.a(arg11)) {
                by.e("label  Invalid!");
                v4 = "__illegal";
            } else {
                v4 = arg11;
            }
        } catch (Exception v0) {
            return true;
        }
        try {
            final HashMap v7 = new HashMap();
            ((Map) v7).put(v1, new l(((List) v1), ((long) arg10), v4, System.currentTimeMillis()));
            bz.b(new cb() {
                @Override
                public void a() {
                    m.a(at.a(at.this)).a(new f() {
                        public void a(Object arg2, boolean arg3) {
                            arg2.equals("success");
                        }
                    }, v7);
                }
            });
            return true;
        } catch (Exception v0) {
            return true;
        }
    }

    static Context a(at arg1) {
        return arg1.e;
    }

    private void a(Context arg4) {
        try {
            String v0_1 = ba.a(arg4).getString("fs_lc_tl", null);
            if (!TextUtils.isEmpty(((CharSequence) v0_1))) {
                this.h = new JSONObject(v0_1);
            }

            this.a();
        } catch (Exception v0) {
        }
    }

    private JSONObject a(Map arg11) {
        int v1_1;
        ArrayList v6;
        Iterator v4;
        JSONObject v3 = new JSONObject();
        try {
            v4 = arg11.entrySet().iterator();
            while (true) {
                while (!v4.hasNext()) {
                    return v3;
                }

                try {
                    Object v0_1 = v4.next();
                    Object v1 = ((Map.Entry) v0_1).getKey();
                    if (v1 == null) {
                        continue;
                    }

                    String v5 = bw.a(((String) v1), 128);
                    v0_1 = ((Map.Entry) v0_1).getValue();
                    if (v0_1 == null) {
                        continue;
                    }

                    if (v0_1.getClass().isArray()) {
                        if ((v0_1 instanceof int[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((int[]) v0_1).length) {
                                ((List) v6).add(Integer.valueOf(((int[]) v0_1)[v1_1]));
                                ++v1_1;
                            }
                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof double[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((double[]) v0_1).length) {
                                ((List) v6).add(Double.valueOf(((double[]) v0_1)[v1_1]));
                                ++v1_1;
                            }
                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof long[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((long[]) v0_1).length) {
                                ((List) v6).add(Long.valueOf(((long[]) v0_1)[v1_1]));
                                ++v1_1;
                            }

                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof float[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((float[]) v0_1).length) {
                                ((List) v6).add(Float.valueOf(((float[]) v0_1)[v1_1]));
                                ++v1_1;
                            }
                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof boolean[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((boolean[]) v0_1).length) {
                                ((List) v6).add(Boolean.valueOf(((boolean[]) v0_1)[v1_1]));
                                ++v1_1;
                            }

                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof byte[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((byte[]) v0_1).length) {
                                ((List) v6).add(Byte.valueOf(((byte[]) v0_1)[v1_1]));
                                ++v1_1;
                            }
                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof short[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((short[]) v0_1).length) {
                                ((List) v6).add(Short.valueOf(((short[]) v0_1)[v1_1]));
                                ++v1_1;
                            }
                            v3.put(v5, v6);
                            continue;
                        }

                        if ((v0_1 instanceof char[])) {
                            v6 = new ArrayList();
                            v1_1 = 0;
                            while (v1_1 < ((char[]) v0_1).length) {
                                ((List) v6).add(Character.valueOf(((char[]) v0_1)[v1_1]));
                                ++v1_1;
                            }
                            v3.put(v5, v6);
                            continue;
                        }

                        v3.put(v5, new ArrayList(Arrays.asList(((Object[]) v0_1))));
                        continue;
                    }

                    if ((v0_1 instanceof String)) {
                        v3.put(v5, bw.a(v0_1.toString(), 256));
                        continue;
                    }

                    v3.put(v5, v0_1);
                    continue;
                } catch (Exception v0) {
                    by.e(((Throwable) v0));
                    continue;
                }
            }
        } catch (Exception e) {
            return v3;
        }
    }

    private void a() {
        int v6 = 10;
        int v0 = 0;
        try {
            if (TextUtils.isEmpty(this.g.a)) {
                return;
            }

            String[] v2 = this.g.a.split("!");
            JSONObject v3 = new JSONObject();
            if (this.h != null) {
                int v1;
                for (v1 = 0; v1 < v2.length; ++v1) {
                    String v4 = bw.a(v2[v1], 128);
                    if (this.h.has(v4)) {
                        v3.put(v4, this.h.get(v4));
                    }
                }
            }

            this.h = new JSONObject();
            if (v2.length >= v6) {
                while (v0 < v6) {
                    this.a(v2[v0], v3);
                    ++v0;
                }
            } else {
                while (v0 < v2.length) {
                    this.a(v2[v0], v3);
                    ++v0;
                }
            }

            this.b(this.e);
            this.g.a = null;
        } catch (Exception v0_1) {
        }
    }

    private void a(String arg3, JSONObject arg4) throws JSONException {
        String v1 = bw.a(arg3, 128);
        if (arg4.has(v1)) {
            this.a(v1, ((Boolean) arg4.get(v1)).booleanValue());
        } else {
            this.a(v1, false);
        }
    }

    private void a(String arg2, boolean arg3) {
        try {
            if ("$st_fl".equals(arg2)) {
                return;
            }

            if ("dplus_st".equals(arg2)) {
                return;
            }

            if ("du".equals(arg2)) {
                return;
            }

            if ("id".equals(arg2)) {
                return;
            }

            if ("ts".equals(arg2)) {
                return;
            }

            if (this.h.has(arg2)) {
                return;
            }

            this.h.put(arg2, arg3);
        } catch (Exception v0) {
        }
    }

    private boolean a(String arg3) {
        if (arg3 != null) {
            try {
                int v0_1 = arg3.trim().getBytes().length;
                if (v0_1 > 0 && v0_1 <= 128) {
                    boolean v0_2 = true;
                    return v0_2;
                }
                by.e("Event id is empty or too long in tracking Event");
            } catch (Exception v0) {
            }
        } else {
            by.e("Event id is empty or too long in tracking Event");
        }

        return false;
    }

    public void a(String arg1, Map arg2) {
    }

    public void b(String arg7, Map arg8) {
        try {
            if (this.a(arg7)) {
                JSONObject v2 = new JSONObject();
                v2.put("id", arg7);
                v2.put("ts", System.currentTimeMillis());
                v2.put("du", 0);
                v2.put("__t", 2050);
                Iterator v3 = arg8.entrySet().iterator();
                int v1;
                for (v1 = 0; v1 < 10; ++v1) {
                    if (!v3.hasNext()) {
                        break;
                    }

                    Object v0_1 = v3.next();
                    if (!"$st_fl".equals(((Map.Entry) v0_1).getKey()) && !"dplus_st".equals(((Map.Entry) v0_1).getKey()) && !"du".equals(((Map.Entry) v0_1).getKey()) && !"id".equals(((Map.Entry) v0_1).getKey()) && !"ts".equals(((Map.Entry) v0_1).getKey())) {
                        Object v4 = ((Map.Entry) v0_1).getValue();
                        if (!(v4 instanceof String) && !(v4 instanceof Integer) && !(v4 instanceof Long)) {
                        } else {
                            v2.put((String) ((Entry) v0_1).getKey(), v4);
                        }
                    }
                }

                v2.put("__i", bd.g(this.e));
                this.i.a(v2);
            }

            return;
        } catch (Throwable v0) {
            return;
        }
    }

    private void b(Context arg4) {
        try {
            if (this.h == null) {
                return;
            }

            ba.a(this.e).edit().putString("fs_lc_tl", this.h.toString()).commit();
        } catch (Exception v0) {
            v0.printStackTrace();
        }
    }

    private boolean b(String arg4) {
        boolean v0 = true;
        if (arg4 != null) {
            try {
                if (arg4.trim().getBytes().length <= 256) {
                    return v0;
                }

                by.e("Event label or value is empty or too long in tracking Event");
            } catch (Exception v0_1) {
            }

            v0 = false;
        }

        return v0;
    }

    private boolean b(Map arg5) {
        if (arg5 != null) {
            try {
                if (arg5.isEmpty()) {
                    by.e("map is null or empty in onEvent");
                    boolean v0_1 = false;
                    return v0_1;
                }

                Iterator v3 = arg5.entrySet().iterator();
                do {
                    if (!v3.hasNext()) {
                        return true;
                    }
                    Object v0_2 = v3.next();
                    if (!this.a(((String) ((Map.Entry) v0_2).getKey()))) {
                        return false;
                    }
                    if (((Map.Entry) v0_2).getValue() == null) {
                        return false;
                    }
                    if (!(((Map.Entry) v0_2).getValue() instanceof String)) {
                        continue;
                    }
                    if (this.b(((Map.Entry) v0_2).getValue().toString())) {
                        continue;
                    }

                    return false;
                }
                while (true);
            } catch (Exception v0) {
                return true;
            }
        }
        by.e("map is null or empty in onEvent");
        boolean v0_1 = false;
        return v0_1;
    }
}

